package net.alan.modules.echart;

import net.alan.common.utils.IOUtils;
import org.springframework.util.ResourceUtils;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author 李岩飞
 * @email eliyanfei@126.com
 * 2017年10月18日 下午2:09:30
 */
public class EchartsUtils {
    /**
     * 从本地文件 /echarts/example/*.txt获取脚本进行替换
     */
    public static String buildEcharts(String name, Map<String, Object> dataMap) {
        final StringBuilder sb = new StringBuilder();
        try {
            List<String> list = IOUtils.readListFromFile(ResourceUtils.getURL("classpath:static/echart/demo/" + name).openStream(), new String[]{"##"});
            for (String s : list) {
                sb.append(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        String data = sb.toString();
        dataMap.put("index", new Random().nextInt(100000));
        for (String key : dataMap.keySet()) {
            data = data.replace("$" + key, String.valueOf(dataMap.get(key)));
        }
        return data;
    }
}